package org.crud4j.ejb3;

import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import org.crud4j.ejb3.example.Student;
import org.crud4j.ejb3.example.Teacher;

public class ExampleEjb3BeanProvider implements Ejb3BeanProvider {

	private static Class[] CLASSES = new Class[] { Teacher.class, Student.class };

	public class Wrapper implements EntityManagerFactory {

		private EntityManagerFactory factory;

		private EntityManager em;

		private Wrapper(EntityManagerFactory factory) {
			super();
			this.factory = factory;
			em = new EntityManagerWrapper(factory.createEntityManager());
		}

		public void close() {
			factory.close();
		}

		public EntityManager createEntityManager() {
			return em;
		}

		public EntityManager createEntityManager(Map arg0) {
			throw new UnsupportedOperationException();
		}

		public boolean isOpen() {
			return factory.isOpen();
		}

	}

	private EntityManagerFactory emf;

	public ExampleEjb3BeanProvider() {
		super();
		emf = new Wrapper(Persistence.createEntityManagerFactory("crud-test"));
	}

	public EntityManagerFactory getEntityManagerFactory() {
		return emf;
	}

	public Class[] listClasses() {
		return CLASSES;
	}

}
